package com.feeyin.radar.crm.controller;

import com.feeyin.radar.commons.DatePropertyEditor;
import com.feeyin.radar.commons.FileUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.form.CustomerLimitForm;
import com.feeyin.radar.crm.form.UploadFileForm;
import com.feeyin.radar.crm.model.QueryCustomer;
import com.feeyin.radar.crm.service.CustomerService;
import com.feeyin.radar.crm.service.OpenCustomerService;
import com.feeyin.radar.sys.service.AssessmentTypeService;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class OpenCustomerController extends MultiActionController
{
  private OpenCustomerService openCustomerService;
  private CustomerService customerService;
  private AssessmentTypeService assessmentTypeService;
  private String openCustomer;
  private String openBatchImport;
  private String openAdvancedQuery;
  private String openBatchImportByLinkman;

  public OpenCustomerService getOpenCustomerService()
  {
    return this.openCustomerService;
  }

  public void setOpenCustomerService(OpenCustomerService openCustomerService) {
    this.openCustomerService = openCustomerService;
  }

  public CustomerService getCustomerService() {
    return this.customerService;
  }

  public void setCustomerService(CustomerService customerService) {
    this.customerService = customerService;
  }

  public AssessmentTypeService getAssessmentTypeService() {
    return this.assessmentTypeService;
  }

  public void setAssessmentTypeService(AssessmentTypeService assessmentTypeService) {
    this.assessmentTypeService = assessmentTypeService;
  }

  public String getOpenCustomer() {
    return this.openCustomer;
  }

  public void setOpenCustomer(String openCustomer) {
    this.openCustomer = openCustomer;
  }

  public String getOpenBatchImport() {
    return this.openBatchImport;
  }

  public void setOpenBatchImport(String openBatchImport) {
    this.openBatchImport = openBatchImport;
  }

  public String getOpenAdvancedQuery() {
    return this.openAdvancedQuery;
  }

  public void setOpenAdvancedQuery(String openAdvancedQuery) {
    this.openAdvancedQuery = openAdvancedQuery;
  }

  public String getOpenBatchImportByLinkman() {
    return this.openBatchImportByLinkman;
  }

  public void setOpenBatchImportByLinkman(String openBatchImportByLinkman) {
    this.openBatchImportByLinkman = openBatchImportByLinkman;
  }

  public ModelAndView display(HttpServletRequest request, HttpServletResponse response, QueryCustomer queryCustomer)
  {
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    Map map = new HashMap();
    map.put("result", request.getParameter("result"));

    String sFlag = request.getParameter("sFlag");
    String customerName = "";
    if ((!StringUtils.isEmpty(sFlag)) && ("1".equals(sFlag))) {
      customerName = request.getParameter("customerName");
    }
    map.put("sFlag", sFlag);
    map.put("customerName", customerName);

    QueryCustomer qCustomer = null;
    if ((!StringUtils.isEmpty(sFlag)) && ("2".equals(sFlag))) {
      qCustomer = queryCustomer;
    }
    map.put("queryCustomer", qCustomer);

    Page page = new Page();
    page.getPageList(this.openCustomerService.getPageOpenCustomersCount(customerName, userId, qCustomer).intValue(), null, request);
    map.put("customers", this.openCustomerService.getPageOpenCustomers(customerName, userId, page, qCustomer));

    CustomerLimitForm customerLimitForm = new CustomerLimitForm();
    customerLimitForm = this.customerService.getCustomerLimit(userId);
    if (this.assessmentTypeService.isExpUser(userId)) {
      map.put("isExpUser", "1");
    }
    map.put("customerLimitForm", customerLimitForm);

    map.put("ranges", this.customerService.getCustomerDictionaryRanges());

    return new ModelAndView(getOpenCustomer(), map);
  }

  public ModelAndView protectCustomer(HttpServletRequest request, HttpServletResponse response)
  {
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    boolean result = false;
    int success = 0;

    String[] ids = request.getParameterValues("id");
    result = this.openCustomerService.protectCustomer(userId, ids);
    if (result) success = 1;

    return new ModelAndView(new RedirectView("./openCustomer.do?action=display&result=" + success));
  }

  public ModelAndView openBatchImport(HttpServletRequest request, HttpServletResponse response)
  {
    return new ModelAndView(getOpenBatchImport());
  }

  public ModelAndView batchImportCustomer(HttpServletRequest request, HttpServletResponse response, UploadFileForm uploadFileForm)
  {
    Map map = new HashMap();

    MultipartFile multipartFile = uploadFileForm.getContents();
    if (multipartFile == null) {
      request.setAttribute("result", Integer.valueOf(2));
      request.setAttribute("uploadFileForm", uploadFileForm);
      return openBatchImport(request, response);
    }
    if (multipartFile.getSize() > 10000000L) {
      request.setAttribute("result", Integer.valueOf(3));
      request.setAttribute("uploadFileForm", uploadFileForm);
      return openBatchImport(request, response);
    }

    Integer userId = (Integer)request.getSession().getAttribute("userId");
    List logs = new ArrayList();
    String realPath = getServletContext().getRealPath("/");
    logs = this.openCustomerService.batchImportCustomer(userId, uploadFileForm, realPath, multipartFile, 0);
    map.put("logs", logs);

    return new ModelAndView(getOpenBatchImport(), map);
  }

  public ModelAndView downloadTemplate(HttpServletRequest request, HttpServletResponse response)
  {
    FileUtils fileUtils = new FileUtils();
    fileUtils.downloadCustomerTemplate(getServletContext().getRealPath("/"), response, 0);
    return null;
  }

  public ModelAndView advancedQueryCustomer(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    map.put("records", this.customerService.getCustomerInfoRecords());
    map.put("ranges", this.customerService.getCustomerDictionaryRanges());

    return new ModelAndView(getOpenAdvancedQuery(), map);
  }

  protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
  {
//    binder.registerCustomEditor([B.class, new ByteArrayMultipartFileEditor());
    binder.registerCustomEditor(Date.class, new DatePropertyEditor());
  }
}